﻿using DevComponents.DotNetBar;
using Model;
using MQTTTool.Controls;
using MQTTTool.MyControls;
using MQTTTool.Util;
using Scancodeupload.util;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MQTTTool
{
	public partial class Form1 : Form
	{
		List<MQConfig> mqconfigs = new List<MQConfig>();
		List<AutoMsgModel> comfilename = new List<AutoMsgModel>();//保存自动回复的数据
		public Form1()
		{
			InitializeComponent();
			InitLastPage();
		}
		/// <summary>
		/// 加载上次保存的错误
		/// </summary>
		private void InitLastPage()
		{
			List<MQConfig> configs = JsonUtil.GetInstance().GetMqttObj<List<MQConfig>>();
			if (configs != null && configs.Count> 0)
			{
				
				configs.ForEach(item=> {
					
					MqttPage page = new MqttPage(item);

					while (tabControl1.SelectNextTab())
					{

					}
					tabControl1.CreateTab(item.sbbh);
					tabControl1.SelectNextTab();
					tabControl1.SelectedPanel.Controls.Add(page);
					mqconfigs.Add(page.config);
				});
			}
			else
			{;
				MqttPage page = new MqttPage();

				while (tabControl1.SelectNextTab())
				{

				}
				tabControl1.CreateTab("Mqtt");
				tabControl1.SelectNextTab();
				tabControl1.SelectedPanel.Controls.Add(page);
				mqconfigs.Add(page.config);

			}
		}


		private void buttonX2_Click(object sender, EventArgs e)
		{
			MqttPage page = new MqttPage();

			while (tabControl1.SelectNextTab())
			{

			}
			tabControl1.CreateTab("Mqtt");
			tabControl1.SelectNextTab();
			tabControl1.SelectedPanel.Controls.Add(page);
			mqconfigs.Add(page.config);
		}

		private void buttonX1_Click(object sender, EventArgs e)
		{
			Process.GetCurrentProcess().Kill();
		}

		//保存自动回复窗口的信息
		private void seveautoMsgform()
        {
           /* DataGridViewRowCollection a = this.dataGridViewX1.Rows;//拿全部行的数据
            List<Object> list = a;

            Hashtable ht = new Hashtable();
            AutoMsgModel auto = new AutoMsgModel();
            for (int i = 0; i < a.Count; i++)
            {
                DataGridViewRow list = a[i];
                String c = list.AccessibilityObject.Value;
                String[] w = c.Split(';');
                c.Substring(0,)
                if (w[0] != string.Empty && w[0] != " " && w[0] != "(新建)") //判断保存的行不为空
                {
                    ht.Add(w[0], w[1]);
                }
            }	*/
        }

		/// <summary>
		/// 保存
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void buttonX4_Click(object sender, EventArgs e)
		{
			JsonUtil.GetInstance().SaveMqttObj(mqconfigs);
		//	JsonUtil.GetInstance().SaveMqttObj(comfilename);
		}

		private void tabControl1_TabItemClose(object sender, TabStripActionEventArgs e)
		{
			TabItem tabitem = e.TabItem;
			tabControl1.SelectedTab = tabitem;
			/*Control con = tabControl1.SelectedPanel.Controls[0];
			if (con is MqttPage)
			{
				MqttPage page = (MqttPage)con;
				page.Mqttutil.DisConnect();
				mqconfigs.Remove(page.config);
			}*/
			tabControl1.Tabs.Remove(tabitem);


		}

		private void Form1_FormClosed(object sender, FormClosedEventArgs e)
		{
			Process.GetCurrentProcess().Kill();

		}

		private void buttonX3_Click(object sender, EventArgs e)
		{
			ComPage page = new ComPage();

			while (tabControl1.SelectNextTab())
			{

			}
			tabControl1.CreateTab("串口助手");
			tabControl1.SelectNextTab();
			tabControl1.SelectedPanel.Controls.Add(page);
		}

		private void timer1_Tick(object sender, EventArgs e)
		{ 
			GC.Collect();
		}

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void buttonX5_Click(object sender, EventArgs e)
        {
			LittleGenius littleGenius = new LittleGenius();
			tabControl1.CreateTab("小天才测试仪");
			tabControl1.SelectNextTab();
			tabControl1.SelectedPanel.Controls.Add(littleGenius);
		}

		private void openSaomiao(Saomiao.ScanerCodes codes) 
		{

		}
    }
}
